package com.nfIt.Dbutils;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * ClossName:LocalDateTimeAdapter
 * Pakage:com.nfIt.jdbcDbutils
 * Description
 *
 * @Author L
 * @Create 2024/3/12 9:09
 * @Version 2023.1.3
 */


/**
 *自定义类型适配器 - 定义序列化和反序列化日期时间格式
 */
public class LocalDateTimeAdapter implements JsonSerializer<LocalDateTime>,JsonDeserializer<LocalDateTime> {

   public static final DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public LocalDateTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        return LocalDateTime.parse(jsonElement.getAsString(), formatter);
    }

    @Override
    public JsonElement serialize(LocalDateTime localDateTime, Type type, JsonSerializationContext jsonSerializationContext) {
        return new JsonPrimitive(formatter.format(localDateTime));
    }

}
